package com.library.service;



import com.library.domain.Author;
import com.library.domain.Book;
import com.library.domain.BookFull;
import com.library.domain.Publisher;
import com.library.mapper.AuthorMapper;
import com.library.mapper.BookMapper;
import com.library.mapper.PublisherMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class BookServiceImpl implements BookService {

    @Autowired
    BookMapper bookMapper;

    @Autowired
    AuthorMapper authorMapper;
    @Autowired
    PublisherMapper publisherMapper;

    @Override
    public List<BookFull> selectAll() {

        return bookMapper.selectAll();
    }

    @Override
    public List<BookFull> selectByIsbn(String isbn) {
        return bookMapper.selectByIsbn(isbn);
    }

    @Override
    public List<BookFull> selectByName(String bookName) {
        return bookMapper.selectByName(bookName);
    }

    @Override
    public List<BookFull> selectByAuthor(String authorName) {
        return bookMapper.selectByAuthor(authorName);
    }

    @Override
    public List<BookFull> selectByPublisher(String pubName) {
        return bookMapper.selectByPublisher(pubName);
    }

    @Override
    public Integer delByIsbn(String isbn) {
        return bookMapper.delByIsbn(isbn);
    }

    @Override
    public Integer add(BookFull bookFull) {

        List<Author> authors = authorMapper.selectByName(bookFull.getAuthorName());
        List<Publisher> publishers = publisherMapper.selectByName(bookFull.getPubName());

        if(authors!=null&&authors.size()>0&&publishers!=null&&publishers.size()>0) {
            Book book = new Book();
            book.setAid(authors.get(0).getAid());
            book.setPid(publishers.get(0).getPid());
            book.setBookName(bookFull.getBookName());
            book.setDescription(bookFull.getDescription());
            book.setPrice(bookFull.getPrice());
            book.setRegDate(bookFull.getRegDate());
            book.setIsbn(UUID.randomUUID().toString());
            return bookMapper.add(book);
        }

        return  0;
    }

    @Override
    public Integer change(BookFull bookFull) {
        List<Author> authors = authorMapper.selectByName(bookFull.getAuthorName());
        List<Publisher> publishers = publisherMapper.selectByName(bookFull.getPubName());
        if(authors!=null&&authors.size()>0&&publishers!=null&&publishers.size()>0) {
            Book book = new Book();
            book.setAid(authors.get(0).getAid());
            book.setPid(publishers.get(0).getPid());
            book.setBookName(bookFull.getBookName());
            book.setDescription(bookFull.getDescription());
            book.setPrice(bookFull.getPrice());
            book.setRegDate(bookFull.getRegDate());
            book.setIsbn(bookFull.getIsbn());
            return bookMapper.change(book);
        }

        return  0;
    }

}
